﻿namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using Moq;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using Xunit;

    public sealed class DebugSubmissionResultFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<DebugSubmissionResult>()
                .DerivesFrom<ActionResult>()
                .IsConcreteClass()
                .IsSealed()
                .NoDefaultConstructor()
                .Result);
        }

        [Fact]
        public void ctor_FormSubmission()
        {
            Assert.NotNull(new DebugSubmissionResult(new FormSubmission()));
        }

        [Fact]
        public void ctor_FormSubmissionNull()
        {
            Assert.Throws<ArgumentNullException>(() => new DebugSubmissionResult(null as FormSubmission));
        }

        [Fact]
        public void op_ExecuteResult_ControllerContextNull()
        {
            Assert.Throws<ArgumentNullException>(() => new DebugSubmissionResult(new FormSubmission()).ExecuteResult(null as ControllerContext));
        }

        [Fact]
        public void op_ExecuteResult_ControllerContext()
        {
            var mock = new Mock<HttpContextBase>();
            mock.Setup(x => x.Response.Cache.SetCacheability(HttpCacheability.NoCache)).Verifiable();
            mock.SetupSet(x => x.Response.ContentType).Callback(expected => Assert.Equal<string>("text/html", expected)).Verifiable();
            mock.Setup(x => x.Response.AppendHeader("Content-Language", "en")).Verifiable();
            mock.Setup(x => x.Response.Write(It.IsAny<string>())).Verifiable();

            FormSubmission submission = new FormSubmission
            {
                Cacheability = HttpCacheability.Server,
                Created = DateTime.UtcNow,
                Data =
                {
                    { "foo", "bar" }
                },
                Etag = "xyz==",
                Expires = DateTime.UtcNow,
                Modified = DateTime.UtcNow,
                Status = HttpStatusCode.OK,
                Token = Token.New(),
                Urn = "urn://example.com/submission/" + Token.New()
            };

            new DebugSubmissionResult(submission).ExecuteResult(new ControllerContext()
            {
                HttpContext = mock.Object
            });

            mock.VerifyAll();
        }
    }
}